package com.itboye.ihomebank.activity.homefragment;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiIndoorResult;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;

public class ActivityNear extends BaseOtherActivity {
	View v_statusbar;
	PoiSearch mPoiSearch;
	// 百度地图控件
	private MapView bmapview;
	// 百度地图对象
	private BaiduMap bdMap;
	private double latitude;
	private double longitude;
	ImageView close_icon;
	TextView add_shap_title_tv, traffic_map_btn, chaoshi_map_btn, school_btn,
			hospital_btn, fangyuan_btn;

	private MyLocationData locData;

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;
		case R.id.traffic_map_btn://公交站
			nearbySearch(10,"公交站");
			break;
		case R.id.chaoshi_map_btn://超市
			nearbySearch(10,"超市");
			break;
		case R.id.school_btn://学校
//			mPoiSearch.searchInCity((new PoiCitySearchOption())
//					.city("杭州")
//					.keyword("学校")
//					.pageNum(10));
			nearbySearch(10,"学校");
			break;
		case R.id.hospital_btn://医院
//			mPoiSearch.searchInCity((new PoiCitySearchOption())
//					.city("拱墅区")
//					.keyword("医院")
//					.pageNum(10));
			nearbySearch(10,"医院");
			break;
		case R.id.fangyuan_btn://小区
			nearbySearch(10,"小区");
			break;
		default:
			break;
		}
	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_near;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub

	}
	private void nearbySearch(int page, String keyWord) {
		PoiNearbySearchOption nearbySearchOption = new PoiNearbySearchOption();
		nearbySearchOption.location(new LatLng(latitude, longitude));
		nearbySearchOption.keyword(keyWord);
		nearbySearchOption.radius(6000);// 检索半径，单位是米
		nearbySearchOption.pageNum(page);
		nearbySearchOption.pageCapacity(50);
		mPoiSearch.searchNearby(nearbySearchOption);// 发起附近检索请求
	}
	@Override
	protected void onInit() {
		// TODO Auto-generated method stub
		setStatusBarHeight(v_statusbar);
		add_shap_title_tv.setText("房源附近");
		bdMap = bmapview.getMap();
		mPoiSearch = PoiSearch.newInstance();

		latitude = getIntent().getDoubleExtra("latitude", 0.000000);
		longitude = getIntent().getDoubleExtra("longitude", 0.000000);
		initLocation();
		nearbySearch(10,"小区");
		mPoiSearch.setOnGetPoiSearchResultListener(poiListener);


	}
	private void initLocation() {
		// TODO Auto-generated method stub
		System.out.println("lng" + longitude + "lat" + latitude);
		LatLng ll = new LatLng(latitude, longitude);
		MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll, 16); // 设置地图中心点以及缩放级别
		bdMap.animateMapStatus(u);

	}
	OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener(){
		public void onGetPoiResult(PoiResult poiResult){
			// 获取POI检索结果
			if (poiResult == null
					|| poiResult.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {// 没有找到检索结果
				Toast.makeText(ActivityNear.this, "未找到结果", Toast.LENGTH_LONG)
						.show();
				return;
			}
			if (poiResult.error == SearchResult.ERRORNO.NO_ERROR) {// 检索结果正常返回
				bdMap.clear();
				PoiOverlay poiOverlay = new PoiOverlay(bdMap);
				poiOverlay.setData(poiResult);// 设置POI数据
				bdMap.setOnMarkerClickListener(poiOverlay);
				poiOverlay.addToMap();// 将所有的overlay添加到地图上
				poiOverlay.zoomToSpan();
				//
				int totalPage = poiResult.getTotalPageNum();// 获取总分页数
				Toast.makeText(
						ActivityNear.this,
						"总共查到" + poiResult.getTotalPoiNum() + "个兴趣点, 分为"
								+ totalPage + "页", Toast.LENGTH_SHORT).show();

			}
		}
		public void onGetPoiDetailResult(PoiDetailResult result){
			//获取Place详情页检索结果
			if (result.error != SearchResult.ERRORNO.NO_ERROR) {
				Toast.makeText(ActivityNear.this, "抱歉，未找到结果",
						Toast.LENGTH_SHORT).show();
			} else {// 正常返回结果的时候，此处可以获得很多相关信息
				Toast.makeText(
						ActivityNear.this,
						result.getName() + ": "
								+ result.getAddress(),
						Toast.LENGTH_LONG).show();
			}

		}

		@Override
		public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {
			Toast.makeText(
					ActivityNear.this,
					poiIndoorResult.getPoiNum(),
					Toast.LENGTH_LONG).show();
		}
	};


	@Override
	protected void onResume() {
		super.onResume();
		bmapview.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
		bmapview.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		try{
			mPoiSearch.destroy();
			// 退出时销毁定位
			bdMap.setMyLocationEnabled(false);
			bmapview.onDestroy();
			bmapview = null;
			mPoiSearch.destroy();
		}catch (Exception ex){}
		// 回收bitmip资源
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub

	}

}
